<?php
/**
 * @version		$Id: weblinks.php 8117 2007-07-20 13:37:22Z friesengeist $
 * @package		Joomla
 * @subpackage	Content
 * @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.model');

/**
 * Weblinks Component Weblink Model
 *
 * @author	Johan Janssens <johan.janssens@joomla.org>
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class BookstoreModelLocator extends JModel
{
	/**
	 * Category ata array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Category total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();

		global $mainframe, $option;

		// Get the pagination request variables
		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart	= $mainframe->getUserStateFromRequest( $option.'limitstart', 'limitstart', 0, 'int' );

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	/**
	 * Method to get weblinks item data
	 *
	 * @access public
	 * @return array
	 */
	 function getQuery(){
		 $query = $this->_buildQuery();
		 return $query;
	 }
	 function getMaps()
	 {
	 	$query = 'SELECT * FROM #__maps';
		$maps = $this->_getList($query);
		return $maps;
	 }
	function getData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$id	= JRequest::getVar( 'id', '', 'get', 'int' );
			$query = $this->_buildQuery($id);
			if(!empty($id)){
				$this->_data = $this->_getList($query);//, $this->getState('limitstart'), $this->getState('limit'));
			}else{
				$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
			}//end if individual query
		}
		//var_dump($this->_data);
		return $this->_data;
	}

	/**
	 * Method to get the total number of weblink items
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the weblinks
	 *
	 * @access public
	 * @return integer
	 */
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

	function _buildQuery($id = '')
	{
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere($id);
		$orderby	= $this->_buildContentOrderBy();

		// @TODO Check for valid data (may need to be done where calls to _buildQuery are sent from)
		$query = ' SELECT a.*, c.title as map_title'
			. ' FROM #__locator AS a '
			. ' LEFT JOIN #__maps AS c ON c.id = a.map_id'
			//. ' LEFT JOIN #__users AS u ON u.id = a.checked_out '
			. $where
			. $orderby
		;
		
		return $query;
	}

	function _buildContentOrderBy()
	{
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		'a.ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );

		if ($filter_order == 'a.ordering'){
			$orderby 	= ' ORDER BY a.dept_cat_desc '.$filter_order_Dir;
		} else {
			$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' , a.dept_cat_desc ';
		}
		return $orderby;
	}

	function _buildContentWhere($id = '')
	{
		global $mainframe, $option;

		$filter_state		= $mainframe->getUserStateFromRequest( $option.'filter_state',		'filter_state',		'',				'word' );
		$filter_catid		= $mainframe->getUserStateFromRequest( $option.'filter_catid',		'filter_catid',		0,				'int' );
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		'a.ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$search				= $mainframe->getUserStateFromRequest( $option.'search',			'search',			'',				'string' );
		$search				= JString::strtolower( $search );
		(empty($id))? $id = JRequest::getVar( 'id', '', 'get', 'int' ) : $id = $id;
		//var_dump($id.'model');
		$where = array();
		//var_dump($id);
		if ($id > 0) {
			$where[] = 'a.id = '.(int) $id;
		}
		if ($search) {
			$where[] = '( LOWER(a.dept_cat_desc) LIKE '.$this->_db->Quote('%'.$search.'%')
					.' || LOWER(a.alias) LIKE '.$this->_db->Quote('%'.$search.'%')
					.')';
		}
		/*
		if ($filter_catid > 0) {
			$where[] = 'a.catid = '.(int) $filter_catid;
		}
		if ($search) {
			$where[] = 'LOWER(a.title) LIKE '.$this->_db->Quote('%'.$search.'%');
		}
		if ( $filter_state ) {
			if ( $filter_state == 'P' ) {
				$where[] = 'a.published = 1';
			} else if ($filter_state == 'U' ) {
				$where[] = 'a.published = 0';
			}
		}
		*/

		$where 		= ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
		return $where;
	}
	
	function store($post = '')
{
	$row =& $this->getTable('Locator');
	
	//var_dump('row 308');
	$data['x'] = JRequest::getVar( 'x' );	
	$data['y'] = JRequest::getVar( 'y' );
	$data['z'] = JRequest::getVar( 'z' );
	$data['map_id'] = JRequest::getVar( 'map_id' );
	$data['dept_cat_desc'] = JRequest::getVar( 'dept_cat_desc' );
	$data['id'] = JRequest::getVar( 'id' );
	$data['alias'] = JRequest::getVar( 'alias' );
	$data['params'] = JRequest::getVar( 'params' );
//	($post == '')? $data = JRequest::get() : $data = $post ;
	$data['description'] = JRequest::getVar( 'description', '', 'post', 'string', JREQUEST_ALLOWRAW );
	//$data['title'] = JRequest::getVar( 'title', '', 'post', 'string', JREQUEST_ALLOWRAW );
//   	$time = JRequest::getVar( 'start_time', '', 'post', 'array' );
	//$data['start_time'] = $this->time_selector($time);
	//$data['created_by']
//	$params		= JRequest::getVar( 'params', '', 'post', 'array' );
		
		/*
		// Build parameter INI string
		if (is_array($params))
		{
			//$time_str = $params['hour'].":".$params['minute'].":".$params['second'];
			//$ampm = ($params['am_pm'] == 0)? 'AM':'PM'; 
			
			//$paramstamp = mktime ( $params['hour'], $params['minute'], $params['second'], int $month [, int $day [, int $year [, int $is_dst]]]]]]] )
			//$param24 = $this->time_selector( array('hours' => $params['hour'], 'minutes' => $params['minute'], 'seconds' => $params['second'], 'am_pm' => $params['am_pm'] ) );
			$data['start_time'] = $param24;
			
			$txt = array ();
			foreach ($params as $k => $v) {
				$txt[] = "$k=$v";
				if($k == 'start_date'){
				$data['start_date'] = $v;
				}
			}
			$txt[] = "time=".$time_str;
			//$row->params = implode("\n", $txt);
			$data['params'] = implode("\n", $txt);
			
		}//end of is_array
		*/
		
	//$this->saveContentPrep( $data );
	
	//$data['publish_up'] = date('Y-m-d G:i:s', time());
	//$data['publish_down'] = $data['start_date']." ".$data['start_time'];
	//$data{'state'} = $data['published'];
	if (!$row->bind($data)) {
        $this->setError($this->_db->getErrorMsg());
        return false;
    }
    // Make sure the hello record is valid
    if (!$row->check()) {
        $this->setError($this->_db->getErrorMsg());
        return false;
    }

    // Store the web link table to the database
    if (!$row->store()) {
        $this->setError($this->_db->getErrorMsg());
        return false;
    }
	
    return true;
}
	
}
?>
